
package com.example.controller;

import com.example.service.SmsCodeService;
import com.example.common.ApiResult;
import com.example.dto.SmsCodeResponse;
import com.example.dto.SmsCodeResquest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/sms")
public class SmsCodeController {

	private final SmsCodeService smsCodeService;

	@Autowired
	public SmsCodeController(SmsCodeService smsCodeService) {
		this.smsCodeService = smsCodeService;
	}

	/**
	 * 发送短信验证码接口
	 * @param params 包含手机号等参数
	 * @return 发送结果
	 */
	@PostMapping("/send")
	public ApiResult<?> sendSmsCode(@RequestBody SmsCodeResquest params) {
	    String mobile = params.getMobile();
		if (mobile == null || mobile.isEmpty()) {
			return ApiResult.error("手机号不能为空");
		}
		SmsCodeResponse result = smsCodeService.sendCode(mobile);
		if ("success".equalsIgnoreCase(result.getResult())) {
			return ApiResult.success("验证码发送成功", result.getCode());
		} else {
			return ApiResult.error("验证码发送失败");
		}
	}
}

